
//about load each js code basing on different dependency
//按不同先后的依赖关系加载各个JS文件
var baseDependencyArray = [
		"jquery",
		"jquery.nav",
		"bootstrap",
		"wow"
	]
if (/msie [6|7|8|9]/i.test(navigator.userAgent)){
	baseDependencyArray.push("html5shiv") //HTML5 shim为IE9和IE8提供HTML5元素
	baseDependencyArray.push("respond") //Respond.js为IE9和IE8提供媒体查询支持
};
define(baseDependencyArray,function ($) {
	$(document).ready(function(){
		//加载等待褪去
		$('#loading').fadeOut('fast','swing',function(){
			$('body').css('overflow','auto');
		});
		
		$(window).scroll(function(){
			//返回顶部按钮显现效果
			if($(window).scrollTop() >= 300){ //向下滚动像素大于这个值时，即出现小火箭~
				$('.toTop').fadeIn(500); //火箭淡入的时间，越小出现的越快~
			}else{    
				$('.toTop').fadeOut(500); //火箭淡出的时间，越小消失的越快~
			}  
	
			//控制顶部导航的透明及位置切换效果
			if ($(window).width() >= 10) {
				if($(window).scrollTop() < 400){
					if($('#mainNenu').data('stick') === true){
						$('#mainNenu').data('stick', false);
						$('#mainNenu').stop(true).animate({opacity : 0}, 300, function(){
							$('#mainNenu').removeClass('fixed');
							$('#mainNenu').stop(true).animate({opacity : 1}, 300);
						});
					}
				}else {
					if($('#mainNenu').data('stick') === false || typeof $('#mainNenu').data('stick') === 'undefined'){
						$('#mainNenu').data('stick', true);
						$('#mainNenu').stop(true).animate({opacity : 0},300,function(){
							$('#mainNenu').addClass('fixed');
							$('#mainNenu.fixed').stop(true).animate({opacity : 1}, 300);
						});
					}
				}
			}
		});
		
		$(window).resize(function(){
			//控制菜单的显示隐藏
			if($(window).width() > 992) $('#menu').show('fast','swing')
			else $('#menu').hide('fast','swing')
		})
		
		//定义页面元素渐入动画
		if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))){
			new WOW().init();
		};
		
		//定义单页导航菜单
		$('#menu nav ul').onePageNav({
				currentClass: 'active'
		});
		
		//点击手机导航的按钮、菜单项显示隐藏菜单
		$('#menuToggle,#menu nav ul li a').click(function(){
			if($(window).width() <= 992) $('#menu').slideToggle('fast','swing')
		})
	});
	
	/*
	图片（包括图片和背景）动画延缓加载的JS
	*/
	$.fn.lazyLoadPic = function(options) {
		var opts = $.extend({
			animate:true,//默认值，是否使用动画
			easing:'swing',//默认值，动画缓动的效果
			duration:500,//默认值，动画缓动持续时间，单位毫秒ms
			func: ""//默认值，动画完成时执行的函数
		},options);
		var picObj = $(this);
		var loadFunc = function(thePic){
			if(opts.animate)thePic.css('opacity',0);
			var actFlag = true;
			//console.log($(window).scrollTop(),thePic.offset().top)
			if(thePic.offset().top <= 0)actLoad(thePic,actFlag)
			$(window).scroll(function(){
				actLoad(thePic,actFlag)
			});
		}
		//执行懒加载的函数
		function actLoad(thePic,actFlag){
			if($(window).scrollTop() > (thePic.offset().top - $(window).height()) && actFlag){
				if(thePic.attr('data-lazyload-pic') != ''){
					if(thePic.context.nodeName == 'IMG'){
						thePic.attr('src',thePic.attr('data-lazyload-pic'));
					}else{
						thePic.css('background-image','url(' + thePic.attr('data-lazyload-pic') + ')');
					}
					if(opts.animate){
						thePic.animate({opacity:1},{ 
							easing: opts.easing, 
							duration: opts.duration,
							complete: function(){
								if(opts.func != '')opts.func();
							},
						});
					}else{
						if(opts.func != '')opts.func();
					}
				}else{
					console.log('图片链接为空！');
				}
				actFlag = false;
			}
		}
		//循环获取对象
		return picObj.each(function() {
			loadFunc($(this));
		});
	};
	
	return $;
});
